import Vue from 'vue'
import Vuex, { mapGetters } from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    dashboard_title1: "",
    dashboard_title2: "",
    dashboard_title3: "",
    dashboard_title4: "",
    menu: [],
    page: {},
    le5le: {},
    timeLineChart: {},
    timeBarChart: {},
    timePieChart: {},
    dataSetName: [
      { id: "gw", name: "网关", webMsgType: 1007 },
      { id: "te", name: "终端", webMsgType: 1008 },
      { id: "yx", name: "遥信", webMsgType: 1009 },
      { id: "yc", name: "遥测", webMsgType: 1010 },
      { id: "ym", name: "遥脉", webMsgType: 1011 },
      { id: "yk", name: "遥控", webMsgType: 1012 },
      { id: "yt", name: "遥调", webMsgType: 1013 },
      { id: "task", name: "定时任务", webMsgType: 1015 },
      { id: "report", name: "报表", webMsgType: 1017 },
      { id: "graph", name: "一次图", webMsgType: 1016 },
      { id: "timeReport", name: "实时报表", webMsgType: 1024 },
    ],
    dataSet: {
      gw: [],
      te: [],
      yx: [],
      yc: [],
      ym: [],
      yk: [],
      yt: [],
      task: [],
      graph: [],
      report: [],
      timeReport: []
    },
    data: {},
    curAlarm: [],
    newAlarm: { count: 0, data: [] },
    widgetReport: [],
    isGetConfig: false,
    isCompleted: false,
  },
  getters: {

  },
  mutations: {
    /** 
     * 时序图模块
     */
    // resetTimeLineChartData(state, id) {
    //   state.timeLineChart[id] = [];
    // },
    // //添加定时时序图数据
    // addTimeLineChartData(state, data) {
    //   // console.log(data, 'add')
    //   if (!state.timeLineChart.hasOwnProperty(data.id)) {
    //     state.timeLineChart[data.id] = [];
    //   }
    //   console.log(data, data.id, data.data.value, 'add')
    //   state.timeLineChart[data.id].push(data.data.value);
    //   // console.log('a', state.addFlag, state.timeLineChart)
    // },
    // //对时序图数据处理
    // removeTimeLineChartData(state, cfg) {
    //   let id = cfg.id;
    //   let hours = cfg.dataPeriod;
    //   console.log('时序图长度', hours)
    //   if (state.timeLineChart.hasOwnProperty(id)) {
    //     let now = new Date();
    //     let start = new Date(0, 0, 0, 0, 0, 0, 0);
    //     let millis = now - start - 3600 * 1000 * hours;
    //     let HoursAgo = new Date(0, 0, 0, 0, 0, 0, millis);
    //     let canRemoveCount = 0;
    //     for (let i = 0; i < state.timeLineChart[id].length; i++) {
    //       let data = state.timeLineChart[id][i];
    //       // console.log(data, 'data')
    //       let timeStr = data[0];
    //       let dataTime = new Date(timeStr);
    //       console.log(dataTime, HoursAgo, 'datatime----')
    //       if (dataTime >= HoursAgo) {
    //         canRemoveCount = i;
    //         console.log(id, "dataTime=", timeStr, dataTime, canRemoveCount);
    //         break;
    //       }
    //     }
    //     if (canRemoveCount > 0) {
    //       state.timeLineChart[id].splice(0, canRemoveCount);
    //     }
    //     // console.log("store 时序图数据：", id, state.timeLineChart[id]);
    //   }
    // },

    /** 
          * 折叠柱状图模块模块
          */
    // resetTimeBarChartData(state, id) {
    //   state.timeBarChart[id] = [];
    // },
    // //添加折叠柱状图数据
    // addTimeBarChartData(state, data) {
    //   // console.log(data, 'add')
    //   if (!state.timeBarChart.hasOwnProperty(data.id)) {
    //     state.timeBarChart[data.id] = [];
    //   }
    //   state.timeBarChart[data.id].push(data.data.value);
    //   // console.log('b', state.addFlag, state.timeBarChart[data.id])
    // },
    // //对折叠柱状图数据处理
    // removeTimeBarChartData(state, cfg) {

    //   let id = cfg.id;
    //   // console.log(state, cfg, state.timeBarChart[id], '删除柱状图数据')
    //   // console.log(id, 'id----')
    //   if (state.timeBarChart.hasOwnProperty(id)) {

    //     let length = state.timeBarChart[id].length;
    //     let canRemoveCount = 0;
    //     if (length > cfg.dataPeriod) {
    //       canRemoveCount = length - cfg.dataPeriod;
    //       state.timeBarChart[id].splice(0, canRemoveCount);
    //       // console.log(state.timeBarChart[id])
    //     }
    //   }
    // },
    /**
     * 
     *饼图模块
     */
    // resetTimePieChartData(state, id) {
    //   state.timePieChart[id] = [];
    // },
    // //添加饼图数据
    // addPieChartData(state, data) {
    //   // console.log(data, 'addPie添加饼图数据')
    //   // console.log(state.timeLineChart.hasOwnProperty(data.id))
    //   if (!state.timePieChart.hasOwnProperty(data.id)) {
    //     state.timePieChart[data.id] = [];
    //   }
    //   state.timePieChart[data.id].push(data.data);
    // },
    // removeTimePieChartData(state, cfg) {
    //   let id = cfg.id;
    //   if (state.timePieChart.hasOwnProperty(id)) {
    //     let length = state.timePieChart[id].length;
    //     let canRemoveCount = 0;
    //     if (length > cfg.dataPeriod) {
    //       canRemoveCount = length - cfg.dataPeriod;
    //       state.timePieChart[id].splice(0, canRemoveCount);
    //       // console.log(state.timeBarChart[id])
    //     }
    //   }
    // },


    localStorage2Vuex(state, data) {
      state = data;
    },
    setMenu(state, data) {
      let pageConfig = {};
      const size = data.length;
      for (let i = 0; i < size; i++) {
        if (data[i].hasOwnProperty('pageConfig')) {
          pageConfig[data[i].id] = data[i].pageConfig;
        }
      }
      state.page = pageConfig;
    },

    setTitle(state, projTitle) {
      switch (projTitle.id) {
        case 'dashboard_title1':
          state.dashboard_title1 = projTitle.value
          break;
        case 'dashboard_title2':
          state.dashboard_title2 = projTitle.value
          break;
        case 'dashboard_title3':
          state.dashboard_title3 = projTitle.value
          break;
        case 'dashboard_title4':
          state.dashboard_title4 = projTitle.value
          break;
        default:
          break;
      }
      state.isCompleted = true;
    },

    // initData(state, data) {
    //   const size = data.length
    //   for (let i = 0; i < size; i++) {
    //     data[i].code = data[i].id
    //     state.le5le[data[i].id] = data[i];
    //   }
    // },
    initData(state, msg) {
      const type = msg.type
      const size = msg.data.length
      state.dataSet[type] = []
      for (let i = 0; i < size; i++) {
        msg.data[i].code = msg.data[i].id
        state.dataSet[type].push(msg.data[i]);
        state.data[msg.data[i].id] = msg.data[i]
      }
    },

    initLe5LeData(state, msg) {
      const type = msg.type
      const size = msg.data.length
      state.dataSet[type] = []
      for (let i = 0; i < size; i++) {
        msg.data[i].code = msg.data[i].id
        state.dataSet[type].push(msg.data[i]);
        if (!state.le5le.hasOwnProperty(msg.data[i].id)) {
          state.le5le[msg.data[i].id] = msg.data[i];
        }
      }
    },

    initLe5LeDataOnce(state, msg) {
      state.le5le[msg.data.id] = msg.data;
    },

    updateData(state, msg) {
      const type = msg.type
      const size = msg.data.length
      for (let i = 0; i < size; i++) {
        if (state.data.hasOwnProperty(msg.data[i].id)) {
          state.data[msg.data[i].id].value = msg.data[i].value
          state.data[msg.data[i].id].update_time = msg.data[i].update_time
        }
      }
    },

    initGateway(state, msg) {
      const type = msg.type
      const size = msg.data.length
      state.dataSet[type] = []
      for (let i = 0; i < size; i++) {
        msg.data[i].id = msg.data[i].gw_sn;
        msg.data[i].name = msg.data[i].gw_name;

        state.dataSet[type].push(msg.data[i]);
        state.data[msg.data[i].gw_sn] = msg.data[i]
      }
    },

    updateGateway(state, msg) {
      const id = msg.data.gw_sn
      state.data[id] = msg.data;
    },

    updateTerminal(state, msg) {
      const size = msg.data.length
      for (let i = 0; i < size; i++) {
        let id = msg.data[i].id
        if (state.data.hasOwnProperty(id)) {
          Object.assign(state.data[id], msg.data[i])
        }
      }
    },

    refreshAlarm(state, msg) {
      state.curAlarm = msg.data
    },

    addNewAlarm(state, msg) {
      state.newAlarm.count = state.newAlarm.data.unshift(msg.data);
    },

    removeNewAlarm(state) {
      state.newAlarm.data = []
      state.newAlarm.count = 0;
    },

    updateWidget(state, data) {
      let pageId = data.id;
      let idx = data.widgetIndex;
      if (state.page.hasOwnProperty(pageId)) {
        let page = state.page[pageId];
        let widget = page.widgets[idx];
        let setup = widget.value.setup;
        let keys = Object.keys(data.setup);
        console.log(keys, 'keys')
        for (let i = 0; i < keys.length; i++) {
          let key = keys[i];
          let value = data.setup[key];
          setup[key] = value;
        }
      }
    },
    addWidgetReportData(state, data) {
      // if (!state.widgetReport.includes(data.id)) {
      //   state.widgetReport.push(data);
      // }
      // if (!state.widgetReport.some(obj => obj.id === data.id)) {
      //   state.widgetReport.push(data);
      // }
      // 查找指定ID的对象的索引
      const index = state.widgetReport.findIndex(item => item.id === data.id);
      if (index !== -1) {
        // 使用展开运算符创建一个新对象，将原始对象的属性与新数据合并
        const updatedObject = { ...state.widgetReport[index], ...data };
        // 使用索引位置替换原始对象
        state.widgetReport[index] = updatedObject;
      } else {
        // 创建一个包含新数据的对象，并添加到数组末尾
        // const newObject = { id:data.id, ...data };
        state.widgetReport.push(data);
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
